import Mock from 'mockjs'
import * as T from './userManagement_type'
import { ApiPath } from './useManagement'

const baseUrl = import.meta.env.VITE_API_BASE_URL

// Mock.mock( url, post/get , 返回的数据)；

function resData(d: any) {
  return {
    code: 200,
    errMsg: '',
    data: d,
    total: 100,
    page: 1,
    page_size: 20,
  }
}

// 创建用户列表数据
function makeUsers() {
  const userList: T.TUserItem[] = Mock.mock({
    'data|10': [{
      'account': '@title',
      'account_status|1': [0, 1],
      'org_code': '@string',
      'org_name': '@string',
      'org_type': '@string',
      'username': '@name',
      'mobile': '@mobile',
      'email': '@email',
      'passwd': '@string'
    }]
  }).data
  return userList
}
// 模拟列表请求
Mock.mock((new RegExp(`${baseUrl}${ApiPath.List}`, 'gi')), 'get', (config: any) => {
  console.log(`[ mock ]=>`, config)
  return resData(makeUsers())
})

// 模拟用户信息
const user = makeUsers()[0]
// 模拟获得用户信息
Mock.mock((new RegExp(`${baseUrl}${ApiPath.GetUserInfo}`, 'gi')), 'get', (config: any) => {
  return resData(user)
})

// 删除用户
Mock.mock((new RegExp(`${baseUrl}${ApiPath.DeleteUser}`, 'gi')), 'get', (config: any) => {
  return resData({})
})


// 创建角色列表数据
function makeRoles() {
  const roleList: T.TRoleItem[] = Mock.mock({
    'data|10': [{
      'id|+1': 1,
      'order|+1': 1,
      'role_desc': '@csentence',
      'role_name': '@cname',
      'status': '@boolean'
    }]
  }).data
  return roleList
}
// 获得角色列表
Mock.mock((new RegExp(`${baseUrl}${ApiPath.RoleList}`, 'gi')), 'get', (config: any) => {
  return resData(makeRoles())
})

// 创建角色菜单数据
function makeRoleMenus() {
  const roleMenus: T.TRoleMenuItem[] = Mock.mock({
    'data|10': [{
      'menu_code|1-20': 1,
      'menu_name': '@ctitle',
      'menu_type': '@cstring'
    }]
  }).data
  console.log(`[ mock roleMenus ]=>`, roleMenus)
  return roleMenus
}
// 获得角色菜单请求
Mock.mock((new RegExp(`${baseUrl}${ApiPath.RoleMenus}`, 'gi')), 'get', (config: any) => {
  return resData(makeRoleMenus())
})

// 创建系统菜单树
function makeSysMenuTree() {
  const sysMenuTree: T.TSysMenuItem[] = Mock.mock({
    data: [
      { children: [1,2,3,4], code: 999, data_type: 'data_type', menu_name: '企业根', order: 1, parent_code: -1, parent_name: '' },

      { children: [5,6,7,8], code: 1, data_type: 'data_type', menu_name: '菜单1', order: 1, parent_code: 999, parent_name: '企业根' },
      { children: [9,10,11,12], code: 2, data_type: 'data_type', menu_name: '菜单2', order: 1, parent_code: 999, parent_name: '企业根' },
      { children: [13,14,15,16], code: 3, data_type: 'data_type', menu_name: '菜单3', order: 1, parent_code: 999, parent_name: '企业根' },
      { children: [17,18,19,20], code: 4, data_type: 'data_type', menu_name: '菜单4', order: 1, parent_code: 999, parent_name: '企业根' },
      
      { children: [], code: 5, data_type: 'data_type', menu_name: '菜单1-1', order: 1, parent_code: 1, parent_name: '菜单1' },
      { children: [], code: 6, data_type: 'data_type', menu_name: '菜单1-2', order: 1, parent_code: 1, parent_name: '菜单1' },
      { children: [], code: 7, data_type: 'data_type', menu_name: '菜单1-3', order: 1, parent_code: 1, parent_name: '菜单1' },
      { children: [], code: 8, data_type: 'data_type', menu_name: '菜单1-4', order: 1, parent_code: 1, parent_name: '菜单1' },
      
      { children: [], code: 9, data_type: 'data_type', menu_name: '菜单2-1', order: 1, parent_code: 2, parent_name: '菜单2' },
      { children: [], code: 10, data_type: 'data_type', menu_name: '菜单2-2', order: 1, parent_code: 2, parent_name: '菜单2' },
      { children: [], code: 11, data_type: 'data_type', menu_name: '菜单2-3', order: 1, parent_code: 2, parent_name: '菜单2' },
      { children: [], code: 12, data_type: 'data_type', menu_name: '菜单2-4', order: 1, parent_code: 2, parent_name: '菜单2' },
      
      { children: [], code: 13, data_type: 'data_type', menu_name: '菜单3-1', order: 1, parent_code: 3, parent_name: '菜单3' },
      { children: [], code: 14, data_type: 'data_type', menu_name: '菜单3-2', order: 1, parent_code: 3, parent_name: '菜单3' },
      { children: [], code: 15, data_type: 'data_type', menu_name: '菜单3-3', order: 1, parent_code: 3, parent_name: '菜单3' },
      { children: [], code: 16, data_type: 'data_type', menu_name: '菜单3-4', order: 1, parent_code: 3, parent_name: '菜单3' },
      
      { children: [], code: 17, data_type: 'data_type', menu_name: '菜单4-1', order: 1, parent_code: 4, parent_name: '菜单4' },
      { children: [], code: 18, data_type: 'data_type', menu_name: '菜单4-2', order: 1, parent_code: 4, parent_name: '菜单4' },
      { children: [], code: 19, data_type: 'data_type', menu_name: '菜单4-3', order: 1, parent_code: 4, parent_name: '菜单4' },
      { children: [], code: 20, data_type: 'data_type', menu_name: '菜单4-4', order: 1, parent_code: 4, parent_name: '菜单4' },
    ]
  }).data
  return sysMenuTree
}
// 获得系统菜单树
Mock.mock((new RegExp(`${baseUrl}${ApiPath.SysMenuTree}`, 'gi')), 'get', (config: any) => {
  return resData(makeSysMenuTree())
})

// 用户角色列表数据
function makeUserRoles() {
  const userRoles: T.TUserRoleItem[] = Mock.mock({
    'data|10': [{
      'order|+1': 1,
      'role_desc': '@title',
      'role_id|+1': 1,
      'role_name': '@name'
    }]
  }).data
  return userRoles
}

// 用户角色列表
Mock.mock((new RegExp(`${baseUrl}${ApiPath.UserRoles}`, 'gi')), 'get', (config: any) => {
  return resData(makeRoles())
})

console.log(`[ mock start ]=>`, 'start')